home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 14372 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: news.spies.com!usenet
  2. From: Erik Max Francis <max@alcyone.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Derivation and calling virtual functions
  5. Date: Fri, 29 Mar 1996 14:56:19 -0800
  6. Organization: Alcyone Systems
  7. Message-ID: <315C6A93.1A0E8CEE@alcyone.com>
  8. References: <graphix.828032689@spiff.cc.iastate.edu>
  9. NNTP-Posting-Host: newton.alcyone.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (X11; I; Linux 1.2.13 i486)
  14.  
  15. Kent A Vander Velden wrote:
  16.  
  17. >   Now, when I call inst.write() it in turn calls Bass::write() which in
  18. > turn calls Base::func().  Is there a way to instead have it call
  19. > Derived::func() if the instance is of type Derived?  I hoped the
  20. > virtual keyword would help in this case.
  21.  
  22. I'm not sure I understand what your problem is.
  23.  
  24. First problem is that Derive::write is _not_ overriding Base::write, because
  25. you've defined it differently.  (A good compiler will warn you that you're
  26. hiding a virtual functions in the base class.)  You've declared Derived::write
  27. as int Derived::write(void), whereas Base::write is void Base::write(void).
  28. (Plus your use C++ comments and semicolons in the wrong place would not even
  29. let this sucker compile.)
  30.  
  31. But to address your question, I think your question comes from a
  32. misunderstanding of how inheritance works.  If you want Derived::write to call
  33. Derived::func, then write it that way.  If it is properly defined as a virtual
  34. function, then Base::write would call Base::func and Derived::write would call
  35. Derived::func.  That's what C++ is for.
  36.  
  37. -- 
  38. Erik Max Francis &tSftDotIotE && http://www.alcyone.com/max && max@alcyone.com
  39. San Jose, California, U.S.A. && 37 20 07 N 121 53 38 W && the 4th R is respect
  40. H.3`S,3,P,3$S,#$Q,C`Q,3,P,3$S,#$Q,3`Q,3,P,C$Q,#(Q.#`-"C`- && 1love && folasade
  41. Omnia quia sunt, lumina sunt. && Dominion, GIGO, GOOGOL, Omega, Psi, Strategem
  42. "Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
  43.